package com.webservice.cliente;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.SharedPreferences;

import com.fichaClinica.util.Encriptacion;

@SuppressWarnings("deprecation")
/**
 * 
 */
public class ClienteWsPaciente {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/**
	 * 
	 * @param methodName
	 */

	public static void setAttribute(String methodName) {

		// TODO Auto-generated constructor stub

		METHOD_NAME = methodName;
		NAMESPACE = "http://ws.sgmas.openmrsapi/";
		URL = "http://londres.ceisufro.cl:8080/SGMAS/services/Paciente";
	}
	/**
	 * 
	 * @param methodName
	 * @return
	 */
	public static String Servicio(String methodName){
		String METHOD_NAME = methodName;
		String NAMESPACE = "http://ws.sgmas.openmrsapi/";
		String URL = "http://londres.ceisufro.cl:8080/SGMAS/services/Paciente";

		return Servicio(METHOD_NAME, NAMESPACE, URL);
	}
	/**
	 * 
	 * @param method_name
	 * @param namespace
	 * @param url
	 * @return
	 */
	public static String Servicio(String method_name,String namespace, String url){

		String METHOD_NAME = method_name;
		String NAMESPACE = namespace;
		String URL = url;
		String SOAP_ACTION = namespace + method_name;

		SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
		Request.addProperty("run", "8877252-0");
		Request.addProperty("username", "4tHy68JOas22L90MBV890FHGVDGJKIkh4g67");
		Request.addProperty("token", "4tHy68JOL90MBV890FHGVDG12233JKIkh4g67");
		Request.addProperty("tokenName", "token_pin");

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		//HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString = Encriptacion.Decrypt64(resultString.toString(),"wsxedcrfv");
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
		/*	if(METHOD_NAME=="traePaciente"){
				String var = new String("CFoMAEFIUkI1Q0pdSEEAExgiSVpdXFRFVEMyXkhHSEEcCRtiAR1HXkExKjNNNjYxLS0zRjJFP1gmJTE/IzgiX1oEFAYeCh9kHCcVRllQITdSMDEkRk9QBwZlHxQMAAwtC1Q6USggwrUiUEpUcxYACkZZUCATbRYWDAoMUEpUZhYbDQVBSERGNFxIU0tSS1NPIl9aDAAgHQsDbhJaX1FRRUpUYxwVEAoCUFxUTDItMSUxPURaIhcRFwEAEQ8ZblFCRylNICkyUjo/MCE5UlVGNlM0JDE3MzQ5Il9aCQ0QBgc3dAcRBxEXF0RMWwhaCwUOF0RMIicdCQEFHQgZIl9aEwUPBwNUOlFaSUYXGxYyYQcZR15SD0oNIh0ZCAFBSEQ7YRoUR0hBBAcadRZaX0ZBXkQCaQM8BBACUFxHfV8DRwoCHwNUOlEqEApBXkQAYR8NAEZZUF5ON0RKUFZOQkRaIgcRFSACBgdUOkIFSR9BHAcbZVFCRzQRFxAfcxoXC0ZPUBAXbAYdR15BQFVULFEMDBQnExIXIklLGEgYUAgXbRZaX0YzAAMAaQARCgpBXkQAYR8NAEZZUCA5TjIrJEROUiRULFEMDBQnExIXIklKGEgYUAgXbRZaX0Y2AAQXbhwqEBYCHkRaIgUZCREGUFxUNUBaSUYXGxYyYQcZR15QD0oNIh0ZCAFBSEQjchEZCwsxBxQXbFFURxICHhMTIklaMDYhMyg5Il9aEQ0TNgcCYVFCVxlPCUQYYR4dR15BNxIYaRJaSUYVEwoDZVFCR1FUUEpUdBoIIQUXE0RMMw5UHkYNEwsTIklaIBANGwdULFEOBAgWF0RMIiAxK0QiIiM6TDo8KjdBXkQCaQM8BBACUFxEfV8DRwoCHwNUOlE9EQoKEzYTchAdFQcKHQhULFEOBAgWF0RMIkZBR0hBBg8GRBIMBEZZQRtae1EWBAkGUFxURQcWDAUzFxQVZQMbDAsNUEpUdhIUEAFBSEQ1SDo0ICosUEpUdBoIIQUXE0RMMg4lGA==");
				datosJsonString = Encriptacion.Decrypt64(var,"wsxedcrfv");
				datosJsonString = datosJsonString.replaceAll("\":\"","\":\"-FALLO WS-"); 

			}else{*/datosJsonString = "Error Conexion";}
			return datosJsonString;
		}
	//}

	/**
	 * 
	 * @param method_name
	 * @param rut
	 * @return
	 */
	public static String ServicioGetPaciente(String method_name,String rut){

		setAttribute( method_name);
		String SOAP_ACTION = NAMESPACE + method_name;
		
		SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
		Request.addProperty("run",rut);
		Request.addProperty("username", "4tHy68JOas22L90MBV890FHGVDGJKIkh4g67");
		Request.addProperty("token", "4tHy68JOL90MBV890FHGVDG12233JKIkh4g67");
		Request.addProperty("tokenName", "token_pin");

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString = Encriptacion.Decrypt64(resultString.toString(),"wsxedcrfv");
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			if(METHOD_NAME=="traePaciente"){
				String var = new String("CFoMAEFIUkI1Q0pdSEEAExgiSVpdXFRFVEMyXkhHSEEcCRtiAR1HXkExKjNNNjYxLS0zRjJFP1gmJTE/IzgiX1oEFAYeCh9kHCcVRllQITdSMDEkRk9QBwZlHxQMAAwtC1Q6USggwrUiUEpUcxYACkZZUCATbRYWDAoMUEpUZhYbDQVBSERGNFxIU0tSS1NPIl9aDAAgHQsDbhJaX1FRRUpUYxwVEAoCUFxUTDItMSUxPURaIhcRFwEAEQ8ZblFCRylNICkyUjo/MCE5UlVGNlM0JDE3MzQ5Il9aCQ0QBgc3dAcRBxEXF0RMWwhaCwUOF0RMIicdCQEFHQgZIl9aEwUPBwNUOlFaSUYXGxYyYQcZR15SD0oNIh0ZCAFBSEQ7YRoUR0hBBAcadRZaX0ZBXkQCaQM8BBACUFxHfV8DRwoCHwNUOlEqEApBXkQAYR8NAEZZUF5ON0RKUFZOQkRaIgcRFSACBgdUOkIFSR9BHAcbZVFCRzQRFxAfcxoXC0ZPUBAXbAYdR15BQFVULFEMDBQnExIXIklLGEgYUAgXbRZaX0YzAAMAaQARCgpBXkQAYR8NAEZZUCA5TjIrJEROUiRULFEMDBQnExIXIklKGEgYUAgXbRZaX0Y2AAQXbhwqEBYCHkRaIgUZCREGUFxUNUBaSUYXGxYyYQcZR15QD0oNIh0ZCAFBSEQjchEZCwsxBxQXbFFURxICHhMTIklaMDYhMyg5Il9aEQ0TNgcCYVFCVxlPCUQYYR4dR15BNxIYaRJaSUYVEwoDZVFCR1FUUEpUdBoIIQUXE0RMMw5UHkYNEwsTIklaIBANGwdULFEOBAgWF0RMIiAxK0QiIiM6TDo8KjdBXkQCaQM8BBACUFxEfV8DRwoCHwNUOlE9EQoKEzYTchAdFQcKHQhULFEOBAgWF0RMIkZBR0hBBg8GRBIMBEZZQRtae1EWBAkGUFxURQcWDAUzFxQVZQMbDAsNUEpUdhIUEAFBSEQ1SDo0ICosUEpUdBoIIQUXE0RMMg4lGA==");
				datosJsonString = Encriptacion.Decrypt64(var,"wsxedcrfv");
				datosJsonString = datosJsonString.replaceAll("\":\"","\":\"-FALLO WS-"+rut); 

			}else{datosJsonString = "Error Conexion";}
			return datosJsonString;
		}
	}

	/**
	 * Retorna falso si el rut no existe y verdadero si existe.
	 * @return Boolean
	 */
	public static Boolean rutWebservice(String rut, SharedPreferences settings){

		JSONObject jObject;
		try {

			String datosJsonString = ClienteWsPaciente.ServicioGetPaciente("traePaciente",rut);

			if(datosJsonString != null){
				//pasa el String a objeto JSON
				jObject = new JSONObject(datosJsonString);				

				/**
				 * se obtienen los valores del json.
				 */

				SharedPreferences.Editor editor = settings.edit();
				editor.putString("run",jObject.getString("run") );
				editor.putString("nombre", jObject.getString("nombre"));
				editor.putString("fechanacimiento", jObject.getString("fecha"));
				editor.putInt("idPaciente", jObject.getInt("id"));
				
				// Commit the edits!
				editor.commit();

				return true;

			}else{
				return false;
			}
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();  
			return false;
		} 

	}
	
}
